import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/toast_util.dart';

class FeedbackPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => FeedbackPageState();
}

class FeedbackPageState extends State<FeedbackPage> {
  String _content;

  TextEditingController _contentController;

  @override
  void initState() {
    super.initState();
    _contentController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _createAppBar(),
      body: ListView(
        children: <Widget>[
          Container(
            height: 300.0,
            margin: const EdgeInsets.all(10.0),
            padding: const EdgeInsets.all(10.0),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(color: Colors.grey),
            ),
            child: TextField(
              maxLines: 5,
              controller: _contentController,
              style: new TextStyle(color: ColorCommon.black1),
              decoration: new InputDecoration.collapsed(
                hintText: "请描述问题帮助我们更快处理您的反馈",
                hintStyle: TextStyle(fontSize: 16.0, color: ColorCommon.grey1),
              ),
              onChanged: (str) {
                _content = str ?? '';
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: Icon(Icons.comment),
        label: Text(
          "提交",
          style: TextStyle(fontSize: 16.0, color: Colors.white),
        ),
        onPressed: () {
          if (_content != null && _content.isNotEmpty) {
            _feedback(_content);
          } else {
            ToastUtil.showMessageToast("请描述问题帮助我们更快处理您的反馈");
          }
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }

  _createAppBar() {
    return new AppBar(
      elevation: 0.0,
      title: new Text("问题反馈"),
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[],
    );
  }

  _feedback(String content) {
    Map<String, String> params = {
      "content": content,
    };

    NetUtil.post(context, Api.feedbacks, (data, message) {
      ToastUtil.showMessageToast(message);
      Navigator.of(context).pop();
    }, params: params, isShowLoading: true);
  }
}
